package com.my.coolq.api;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.my.coolq.api.request.AddFriendRequest;
import com.my.coolq.api.request.AddGroupRequest;
import com.my.coolq.api.request.GroupBanRequest;
import com.my.coolq.api.request.GroupKickRequest;
import com.my.coolq.api.request.GroupLeaveRequest;
import com.my.coolq.api.request.GroupMemberInfoRequest;
import com.my.coolq.api.request.GroupMemberListRequest;
import com.my.coolq.api.request.GroupMsgRequest;
import com.my.coolq.api.request.GroupWholeBanRequest;
import com.my.coolq.api.request.SetGroupAdminRequest;
import com.my.coolq.api.request.SetGroupAnonymousRequest;
import com.my.coolq.api.request.SetGroupCardRequest;
import com.my.coolq.api.request.SetGroupSpecialTitleRequest;
import com.my.coolq.api.request.StrangerInfoRequest;
import com.my.coolq.api.request.UserMsgRequest;
import com.my.coolq.api.response.BaseResponse;
import com.my.coolq.api.response.GroupListResponse;
import com.my.coolq.api.response.GroupMemberInfoResponse;
import com.my.coolq.api.response.StrangerInfoResponse;
import com.my.coolq.api.response.UserLoginInfoResponse;
import com.my.coolq.constant.CoolqAPIConstant;
import com.my.coolq.constant.CoolqCQConstant;
import com.my.util.HttpUtil;
/**
 * 酷q http api接口
 * @author ch
 *
 */
@Component
public class CoolqApi {
	
	private static final String url ="http://127.0.0.1:5700/";
	
	/**
	 * 获取 Cookies
	 * @return
	 * @throws Exception
	 */
	public BaseResponse getCookies() throws Exception{
		return HttpUtil.httpGet(url+CoolqAPIConstant.get_cookies,BaseResponse.class);
	}
	
	/**
	 * 获取 CSRF Token
	 * @return
	 * @throws Exception
	 */
	public BaseResponse getCSRFToken() throws Exception{
		return HttpUtil.httpGet(url+CoolqAPIConstant.get_csrf_token,BaseResponse.class);
	}
	

	/**
	 * 获取登录信息
	 * @return
	 * @throws Exception
	 */
	public UserLoginInfoResponse getLoginInfo() throws Exception{
		BaseResponse response = HttpUtil.httpGet(url+CoolqAPIConstant.login_info,BaseResponse.class);
		UserLoginInfoResponse data = JSON.parseObject(response.getData(), UserLoginInfoResponse.class);
		return data;
	}
	
	/**
	 * 获取陌生人信息
	 * @param req
	 * @return StrangerInfoResponse
	 * @throws Exception
	 */
	public StrangerInfoResponse getStrangerInfo(StrangerInfoRequest req) throws Exception{
		BaseResponse response = HttpUtil.httpPost(url+CoolqAPIConstant.get_stranger_info,req.toString(),BaseResponse.class);
		StrangerInfoResponse data = JSON.parseObject(response.getData(), StrangerInfoResponse.class);
		return data;
	}
	
	/**
	 * 处理加好友请求
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse addFriend(AddFriendRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.add_friend,req.toString(),BaseResponse.class);
	}
	
	/**
	 * 发送用户消息
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse sendUserMsg(UserMsgRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.send_user_msg,req.toString(),BaseResponse.class);
	}
	
	/**
	 * 赞好友
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse likeFriend(GroupMsgRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.like_friend,req.toString(),BaseResponse.class);
	}
	
	/**
	 * 获取群列表
	 * @return List
	 * @throws Exception
	 */
	public List<GroupListResponse> getGroupList() throws Exception{
		BaseResponse response = HttpUtil.httpGet(url+CoolqAPIConstant.get_group_list,BaseResponse.class);
		List<GroupListResponse> datas = JSON.parseArray(response.getData(), GroupListResponse.class);
		return datas;
	}
	
	/**
	 * 获取群列表
	 * @return List
	 * @throws Exception
	 */
	public List<GroupListResponse> getGroupList(Long group_id) throws Exception{
		Map<String, Long> map = Maps.newHashMap();
		map.put("group_id", group_id);
		BaseResponse response = HttpUtil.httpPost(url+CoolqAPIConstant.get_group_list,JSON.toJSONString(map),BaseResponse.class);
		List<GroupListResponse> datas = JSON.parseArray(response.getData(), GroupListResponse.class);
		return datas;
	}
	
	/**
	 * 获取群成员信息
	 * @param req
	 * @return GroupMemberInfoResponse
	 * @throws Exception
	 */
	public GroupMemberInfoResponse getGroupMemberInfo(GroupMemberInfoRequest req) throws Exception{
		BaseResponse response = HttpUtil.httpPost(url+CoolqAPIConstant.get_group_member_info,req.toString(),BaseResponse.class);
		GroupMemberInfoResponse data = JSON.parseObject(response.getData(), GroupMemberInfoResponse.class);
		return data;
	}
	
	/**
	 * 获取群成员列表
	 * @param req
	 * @return GroupMemberInfoResponse
	 * @throws Exception
	 */
	public List<GroupMemberInfoResponse> getGroupMemberList(GroupMemberListRequest req) throws Exception{
		BaseResponse response = HttpUtil.httpPost(url+CoolqAPIConstant.get_group_member_list,req.toString(),BaseResponse.class);
		List<GroupMemberInfoResponse> datas = JSON.parseArray(response.getData(), GroupMemberInfoResponse.class);
		return datas;
	}
	
	/**
	 * 发送群消息
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse sendGroupMsg(GroupMsgRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.send_group_msg,req.toString(),BaseResponse.class);
	}
	
	/**
	 * 处理加群请求／邀请
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse addGroup(AddGroupRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.add_group,req.toString(),BaseResponse.class);
	}

	
	/**
	 * 群组踢人
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse groupKick(GroupKickRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.group_kick,req.toString(),BaseResponse.class);
	}
	
	
	/**
	 * 群组单人禁言
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse setGroupBan(GroupBanRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.group_ban,req.toString(),BaseResponse.class);
	}
	
	/**
	 * 群组全员禁言
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse setGroupWhole(GroupWholeBanRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.group_whole_ban,req.toString(),BaseResponse.class);
	}
	
	/**
	 * 设置群管理员
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse setGroupWhole(SetGroupAdminRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.set_group_admin,req.toString(),BaseResponse.class);
	}
	
	/**
	 * 设置群匿名
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse setGroupAnonymous(SetGroupAnonymousRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.set_group_anonymous,req.toString(),BaseResponse.class);
	}
	
	/**
	 * 设置群名片（群备注）
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse setGroupCard(SetGroupCardRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.set_group_card,req.toString(),BaseResponse.class);
	}
	
	/**
	 * 退群
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse groupleave(GroupLeaveRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.group_leave,req.toString(),BaseResponse.class);
	}
	
	/**
	 * 设置群组专属头衔
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public BaseResponse setGroupSpecialTitle(SetGroupSpecialTitleRequest req) throws Exception{
		return HttpUtil.httpPost(url+CoolqAPIConstant.set_group_special_title,req.toString(),BaseResponse.class);
	}

	
	
	public static void main(String[] args) throws Exception {
		CoolqApi api = new CoolqApi();
		System.out.println(api.getGroupList(473340262l));
		//System.out.println(api.getCookies());
		//System.out.println(api.getCSRFToken());
		//System.out.println(api.getGroupList());
		//List<String> l = Lists.newArrayList(String.format(CoolqCQConstant.at_qq,673976475),"hah");
		//System.out.println(api.sendGroupMsg(new GroupMsgRequest(473340262L,JSON.toJSONString( l))));
		//System.out.println(api.sendGroupMsg(new GroupMsgRequest(473340262L,StringUtils.join(new String[]{String.format(CoolqCQConstant.at_qq,673976475),"哈哈"}, " "))));
		//System.out.println(api.getGroupMemberList(new GroupMemberListRequest(255482257L)));
	//	System.out.println(new CoolqApi().getLoginInfo());
		//System.out.println(new CoolqApi().sendUserMsg(new UserMsgRequest(673976475l, "哈哈哈",Boolean.TRUE)));
		//System.out.println(new CoolqApi().sendGroupMsg(new GroupMsgRequest(473340262l, "哈哈哈")));
	}
	
}
